﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Main_ServiceInvoice_DebitNoteReport : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable dtGetFullDetail = new DataTable();

    int rowcount = 0;
    string strGrandTotal = "";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            BindCompDetails();
            BindDetails();
            BindFullDetail();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }
    }
    protected void BindCompDetails()
    {
        DataTable dtCompDtl = DU.getdatausingprocedure("usp_get_company_details");
        string strCompDtl = "<b>Supplier</b><br />";
        string strCompNoDtl = "";
        if (dtCompDtl.Rows.Count > 0)
        {
            if (dtCompDtl.Rows[0]["compname"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["compname"].ToString() + "<br />";
                lblCompname.Text = dtCompDtl.Rows[0]["compname"].ToString();
            }
            if (dtCompDtl.Rows[0]["add1"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["add1"].ToString();
            }
            if (dtCompDtl.Rows[0]["add2"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["add2"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["add3"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["add3"].ToString();
            }
            if (dtCompDtl.Rows[0]["add4"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["add4"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["place"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["place"].ToString();
            }
            if (dtCompDtl.Rows[0]["pincode"].ToString() != string.Empty)
            {
                strCompDtl += "-" + dtCompDtl.Rows[0]["pincode"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["state"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["state"].ToString();
            }
            if (dtCompDtl.Rows[0]["country"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["country"].ToString() + ".<br />";
            }
            if (dtCompDtl.Rows[0]["Phone"].ToString() != string.Empty)
            {
                strCompDtl += "Phone-" + dtCompDtl.Rows[0]["Phone"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["Email"].ToString() != string.Empty)
            {
                strCompDtl += "Email-" + dtCompDtl.Rows[0]["Email"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["LogoUrl"].ToString() != string.Empty)
            {
                imgLogo.ImageUrl = dtCompDtl.Rows[0]["LogoUrl"].ToString();
            }
            if (dtCompDtl.Rows[0]["sertno"].ToString() != string.Empty)
            {
                lblServiceTaxNo.Text += dtCompDtl.Rows[0]["sertno"].ToString();
            }
        }
        lblSupplier.Text = strCompDtl;
    }
    protected void BindDetails()
    {
        if (Request.QueryString != null)
        {
            if (Request.QueryString["DEBITNOTEID"].ToString() != string.Empty && Request.QueryString["TRANID"] != string.Empty)
            {
                string credtnote = Request.QueryString["DEBITNOTEID"].ToString();
                string tran = Request.QueryString["TRANID"].ToString();
                SqlParameter[] sqlparam = {
                                               new SqlParameter("@debitnoteid",credtnote),
                                               new SqlParameter("@tranid",tran)
                                          };
                DataTable DTMain = DU.getdatausingprocedure("USP_IVC_GetDebitNote_Details", sqlparam);
                string invoiceto = "<b>Invoice To</b><br />";
                if (DTMain.Rows.Count > 0)
                {
                    if (DTMain.Rows[0]["debitnoteno"].ToString() != string.Empty)
                    {
                        lblCreditNoteNo.Text = DTMain.Rows[0]["debitnoteno"].ToString();
                    }
                    if (DTMain.Rows[0]["voudate"].ToString() != string.Empty)
                    {
                        lblDate.Text = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(DTMain.Rows[0]["voudate"].ToString()));
                    }
                    if (DTMain.Rows[0]["total"].ToString() != string.Empty)
                    {
                        lblGrandTotal.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["total"].ToString()));
                        strGrandTotal = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["total"].ToString()));
                    }
                    if (DTMain.Rows[0]["customer"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["customer"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["Address1"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["Address1"].ToString();
                    }
                    if (DTMain.Rows[0]["Address2"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["Address2"].ToString() + ",<br />";
                    }
                    if (DTMain.Rows[0]["Address3"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["Address3"].ToString();
                    }
                    if (DTMain.Rows[0]["place"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["place"].ToString();
                    }
                    if (DTMain.Rows[0]["Pincode"].ToString() != string.Empty)
                    {
                        invoiceto += "-" + DTMain.Rows[0]["Pincode"].ToString() + ",<br />";
                    }
                    if (DTMain.Rows[0]["state"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["state"].ToString();
                    }
                    if (DTMain.Rows[0]["Country"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["Country"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["EMAIL"].ToString() != string.Empty)
                    {
                        invoiceto += "Email-" + DTMain.Rows[0]["EMAIL"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["PHONE"].ToString() != string.Empty)
                    {
                        invoiceto += "Phone-" + DTMain.Rows[0]["PHONE"].ToString();
                    }
                    if (DTMain.Rows[0]["GTInwords"].ToString() != string.Empty)
                    {
                        lblAmountInWords.Text = " " + strGrandTotal + "/-(" + DTMain.Rows[0]["GTInwords"].ToString() + ")";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["logoyn"].ToString()))
                    {
                        imgLogo.Visible = true;
                    }
                    else
                    {
                        imgLogo.Visible = false;
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["INFORMATIONYN"].ToString()))
                    {
                        lblInformation.Text = DTMain.Rows[0]["INFORMATION"].ToString();
                    }
                    else
                    {
                        lblInformation.Text = "Amount Chargeable (In words)";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["FIRMNAMEADDRYN"].ToString()) != true)
                    {
                        lblInvoiceTo.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["CNAMEADDRYN"].ToString()) != true)
                    {
                        lblSupplier.Text = "";
                        //lblInvoiceFor.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["STREGNOYN"].ToString()) != true)
                    {
                        lblServiceTaxNo.Text = "";
                    }
                    if (DTMain.Rows[0]["EDUCESS2RATE"].ToString() != string.Empty)
                    {
                        lblHECessRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS2RATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["EDUCESS1RATE"].ToString() != string.Empty)
                    {
                        lblPECessRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS1RATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["SERVICETAXRATE"].ToString() != string.Empty)
                    {
                        lblServiceTaxRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["SERVICETAXRATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["CHECKEDBY"].ToString() != string.Empty)
                    {
                        lblCheckedBy.Text = DTMain.Rows[0]["CHECKEDBY"].ToString();
                    }
                    if (DTMain.Rows[0]["PREPAREDBY"].ToString() != string.Empty)
                    {
                        lblPreparedBy.Text = DTMain.Rows[0]["PREPAREDBY"].ToString();
                    }
                    if (DTMain.Rows[0]["EDUCESS2RATE"].ToString() != string.Empty)
                    {
                        lblHECessRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS2RATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["EDUCESS1RATE"].ToString() != string.Empty)
                    {
                        lblPECessRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS1RATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["SERVICETAXRATE"].ToString() != string.Empty)
                    {
                        lblServiceTaxRate.Text = string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["SERVICETAXRATE"].ToString())) + "%";
                    }
                    if (DTMain.Rows[0]["CHECKEDBY"].ToString() != string.Empty)
                    {
                        lblCheckedBy.Text = DTMain.Rows[0]["CHECKEDBY"].ToString();
                    }
                    if (DTMain.Rows[0]["PREPAREDBY"].ToString() != string.Empty)
                    {
                        lblPreparedBy.Text = DTMain.Rows[0]["PREPAREDBY"].ToString();
                    }
                    if (Convert.ToDouble(DTMain.Rows[0]["TAXABLEAMT"].ToString()) > 0)
                    {
                        lblTotalTaxable.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["TAXABLEAMT"].ToString()));
                    }
                    else
                    {
                        lblTotalTaxable.Text = "0.00";
                    }
                    if (Convert.ToDouble(DTMain.Rows[0]["NONTAXABLEAMT"].ToString()) > 0)
                    {
                        lblTotalNonTaxable.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["NONTAXABLEAMT"].ToString()));
                    }
                    else
                    {
                        lblTotalNonTaxable.Text = "0.00";
                    }

                    if (Convert.ToDouble(DTMain.Rows[0]["SERVICETAXAMT"].ToString()) > 0)
                    {
                        lblTotalServiceTaxAmt.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["SERVICETAXAMT"].ToString()));
                    }
                    else
                    {
                        lblTotalServiceTaxAmt.Text = "0.00";
                    }

                    if (Convert.ToDouble(DTMain.Rows[0]["EDUCESS1AMT"].ToString()) > 0)
                    {
                        lblTotalPECessAmt.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS1AMT"].ToString()));
                    }
                    else
                    {
                        lblTotalPECessAmt.Text = "0.00";
                    }

                    if (Convert.ToDouble(DTMain.Rows[0]["EDUCESS2AMT"].ToString()) > 0)
                    {
                        lblTotalHECessAmt.Text = "<img src='../../../Images/RupeeSymbol.png' />&nbsp;" + string.Format("{0:0.00}", Convert.ToDouble(DTMain.Rows[0]["EDUCESS2AMT"].ToString()));
                    }
                    else
                    {
                        lblTotalHECessAmt.Text = "0.00";
                    }
                    if (DTMain.Rows[0]["DEBITENOTETOWARDS"].ToString() != string.Empty)
                    {
                        if (DTMain.Rows[0]["DEBITENOTETOWARDS"].ToString() == "0")
                        {
                            lbltowards.Text = "Difference In Bill";
                        }
                        else if (DTMain.Rows[0]["DEBITENOTETOWARDS"].ToString() == "1")
                        {
                            lbltowards.Text = "Expense Reiumbersment";
                        }
                        else if (DTMain.Rows[0]["DEBITENOTETOWARDS"].ToString() == "2")
                        {
                            lbltowards.Text = "Service Tax Only";
                        }
                    }
                    lblInvoiceTo.Text = invoiceto;
                }
            }
        }

    }
    protected void BindFullDetail()
    {
        if (Request.QueryString != null)
        {
            if (Request.QueryString["DEBITNOTEID"].ToString() != string.Empty)
            {
                string credtnote = Request.QueryString["DEBITNOTEID"].ToString();
                SqlParameter[] sqlparam = {
                                              new SqlParameter("@debitnoteid",credtnote)
                                          };
                dtGetFullDetail = DU.getdatausingprocedure("USP_IVC_GetDebitNote_FullDetails", sqlparam);
                AddNewRow();
            }
        }
    }
    protected void AddNewRow()
    {
        int tempcounter = Convert.ToInt32(dtGetFullDetail.Rows.Count);
       
        if (dtGetFullDetail.Rows.Count > 0)
        {
            for (int i = 0; i < tempcounter; i++)
            {
                rowcount++;
                HtmlTableRow row = new HtmlTableRow();

                HtmlTableCell cell0 = new HtmlTableCell();
                cell0.Align = "top";
                cell0.Style.Add("border", "1px solid #818181");
                cell0.BorderColor = "#000000";

                Label lblSNo = new Label();
                lblSNo.ID = "lblSno" + rowcount.ToString();
                lblSNo.Text = (i + 1).ToString();
                cell0.Controls.Add(lblSNo);

                //HtmlTableCell cell1 = new HtmlTableCell();
                //cell1.Align = "top";
                //cell1.Style.Add("border", "1px solid #818181");
                //cell1.BorderColor = "#000000";

                //Label LblParticular = new Label();
                //LblParticular.ID = "LblParticular" + rowcount.ToString();
                //if (dtGetFullDetail.Rows[i]["particular"].ToString() != string.Empty)
                //{
                //    LblParticular.Text = dtGetFullDetail.Rows[i]["particular"].ToString();
                //}
                //cell1.Controls.Add(LblParticular);

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.Align = "top";
                cell2.Style.Add("border", "1px solid #818181");
                cell2.BorderColor = "#000000";

                Label LblService = new Label();
                LblService.ID = "LblService" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["service"].ToString() != string.Empty)
                {
                    LblService.Text = dtGetFullDetail.Rows[i]["service"].ToString();
                }
                cell2.Controls.Add(LblService);


                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.Align = "left";
                cell3.Style.Add("border", "1px solid #818181");
                cell3.BorderColor = "#000000";

                Label LblDescription = new Label();
                LblDescription.ID = "LblDescription" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["Description"].ToString() != string.Empty)
                {
                    LblDescription.Text = dtGetFullDetail.Rows[i]["Description"].ToString();
                }

                cell3.Controls.Add(LblDescription);

                HtmlTableCell cell8 = new HtmlTableCell();
                cell8.Align = "right";
                cell8.Style.Add("border", "1px solid #818181");
                cell8.BorderColor = "#000000";

                Label lblTtlAmt = new Label();
                lblTtlAmt.ID = "lblTtlAmt" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["grossamt"].ToString() != string.Empty)
                {
                    lblTtlAmt.Text = string.Format("{0:0.00}", Convert.ToDouble(dtGetFullDetail.Rows[i]["grossamt"].ToString()));
                }
                cell8.Controls.Add(lblTtlAmt);

                row.Cells.Add(cell0);
                //row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);

                row.Cells.Add(cell8);

                tblDetail.Rows.Add(row);
                hidControlCounter.Value = rowcount.ToString();

            }
        }

        int rslt = 21 - tempcounter;
        if (rslt > 0)
        {
            for (int j = 0; j <= rslt; j++)
            {
                HtmlTableRow row1 = new HtmlTableRow();

                HtmlTableCell cel0 = new HtmlTableCell();
                cel0.Align = "top";
                cel0.InnerHtml = "&nbsp;";
                cel0.Style.Add("border-left", "solid 1px #818181");
                cel0.Style.Add("border-right", "solid 1px #818181");
                cel0.BorderColor = "#ffffff";

                //HtmlTableCell cel1 = new HtmlTableCell();
                //cel1.Align = "top";
                //cel1.InnerHtml = "&nbsp;";
                //cel1.Style.Add("border-right", "solid 1px #818181");
                //cel1.BorderColor = "#ffffff";

                HtmlTableCell cel2 = new HtmlTableCell();
                cel2.Align = "top";
                cel2.InnerHtml = "&nbsp;";
                cel2.Style.Add("border-right", "solid 1px #818181");
                cel2.BorderColor = "#ffffff";

                HtmlTableCell cel3 = new HtmlTableCell();
                cel3.Align = "right";
                cel3.InnerHtml = "&nbsp;";
                cel3.Style.Add("border-right", "solid 1px #818181");
                cel3.BorderColor = "#ffffff";

                HtmlTableCell cel4 = new HtmlTableCell();
                cel4.Align = "right";
                cel4.InnerHtml = "&nbsp;";
                cel4.Style.Add("border-right", "solid 1px #818181");
                cel4.BorderColor = "#ffffff";

                if (tblDetail.Rows.Count > 21)
                {
                    cel0.Style.Add("border-bottom", "solid 1px #818181");
                    //cel1.Style.Add("border-bottom", "solid 1px #818181");
                    cel2.Style.Add("border-bottom", "solid 1px #818181");
                    cel3.Style.Add("border-bottom", "solid 1px #818181");
                    cel4.Style.Add("border-bottom", "solid 1px #818181");
                }
                row1.Cells.Add(cel0);
                //row1.Cells.Add(cel1);
                row1.Cells.Add(cel2);
                row1.Cells.Add(cel3);
                row1.Cells.Add(cel4);

                tblDetail.Rows.Add(row1);
            }
        }
    }
}


